<?php
	function f111($sql){
		//GRESKA U ORIGINALNOM UPITU DA SE VIDI SA ZELJKOM
		//data handle instance
		//set request
		//define format
		//set format
		//define query string
		//process time
		//process fitler
		$dataHandle = new DataHandle();
		$dataHandle->setRequest("111");

/*		$format = array(
			"date" => "datem/d/Y",
			"fromProduct" => "numb6",
			"toProduct" => "numb6",
			"startTime" => "dateg:i",
			"endTime" => "dateg:i",
			"modifiedDate" => "datem/d/Y",
			);
		$dataHandle->setFormat($format);*/


		$dataType = getValue($_GET['dataType']);

		if ($dataType == "json"){
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "mix_Produced_Mixer","start_time");
				$query = getGetItemsQuery($startTime, $endTime, $filter);
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else{
			$temp = array();
			$temp['error'] = "111: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);

	}

	function getGetItemsQuery(&$startTime, &$endTime, &$filter){

		$queryString = "
			SELECT
				'result' as Item,
				mix_Products.product_id as Recipe,
				mix_Products.description as Description,
				mix_Produced_Mixer.mixer_id AS 'Mixer',
				mix_Produced_Mixer.Batch_Size AS 'BatchSize', 
				mix_Produced_Mixer.Dumping AS 'Dumping',
				mix_Produced_Mixer.Draining AS 'Draining',
				mix_Produced_Mixer.Dumping
				+ mix_Produced_Mixer.Draining as Total
				
			FROM 
				mix_Produced_Summary_View inner join mix_Produced_Mixer 
				on mix_Produced_Summary_view.id_shift = mix_Produced_Mixer.id_shift 
				inner join mix_Products 
				on mix_Produced_Mixer.product_id = mix_Products.product_id ";

			$queryString = $queryString.$filter;

			$queryString = $queryString." union ";

			$queryString = $queryString."
			SELECT
				'total' as Item,
				null as Recipe,
				null as Description,
				null AS 'Mixer',
				null AS 'BatchSize', 
				sum(mix_Produced_Mixer.Dumping) AS 'Dumping',
				sum(mix_Produced_Mixer.Draining) AS 'Draining',
				sum(mix_Produced_Mixer.Dumping
				+ mix_Produced_Mixer.Draining) as Total
				
			FROM 
				mix_Produced_Summary_View inner join mix_Produced_Mixer 
				on mix_Produced_Summary_view.id_shift = mix_Produced_Mixer.id_shift 
				inner join mix_Products 
				on mix_Produced_Mixer.product_id = mix_Products.product_id";

		$queryString = $queryString.$filter;

		return $queryString;
	}
?>